/**
 * 获取url中的参数
 * 
 * @param name
 * @returns
 */
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); // 匹配目标参数
	if (r != null)
		return unescape(r[2]);
	return null; // 返回参数值
}
/**
 * 批注
 */
var commentList = {
	get : function() {
		var pid = $("#pid").val();
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"pid" : pid
			},
			url : "/app/comment!listComment",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state = "success") {
					commentList.show(data.msg, data.userId, data.userName);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	show : function(data, userId, userName) {
		var pm = $("table");
		var content = "";
		pm.empty();
		if (data != null && data.length != 0) {
			content = "<tr id='titleTr'><th width='100px' scope='col'>用户名</th><th width='370px' scope='col'>内容</th>"
					+ "<th width='130px' scope='col'>时间</th><th width='100px' scope='col'>操作</th></tr>";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var contentv = v.content;
				var uName = v.userName;
				var uid = v.userId;
				var time = v.inputTime.substring(0,19);
				
				if(uName == userName){
					uName = "我";
				}

				content += "<tr><td align='center' width='100px'>" + uName
						+ "</td><td align='center' width='370px'>" + contentv
						+ "</td><td align='center' width='130px'>" + time
						+ "</td><td align='center' width='100px'>";
				if (uid == userId) {
					content += "<a href='javascript:commentUpdate.show(" + id + ",\"" + contentv
							+ "\")'>修改</a><a href='javascript:commentDel(" + id + ")'>删除</a>";
				}
				content += "</td></tr>";
			}
		} else {
			content = "<tr><td align='center'><h1>暂无批示</h1></td></tr>";
		}
		pm.append(content);
	}
};
var commentUpdate = {
	show : function(cid, content) {
		art.dialog({
			lock : true,
			id : "updateComment",
			title : '修改批示',
			content : "<span style='vertical-align: top;'>批示内容：</span>"
					+ "<textarea name='commentUpdate' cols='40' rows='10' id='commentUpdate' "
					+ "onkeyup='this.value=this.value.slice(0, 200)'>" + content + "</textarea>",
			width : '30em',
			button : [ {
				name : '确定',
				callback : function() {
					var nowComment = $("#commentUpdate").val();
					if (comment == nowComment) {
						this.close();
					} else {
						commentUpdate.update(cid, nowComment);
					}
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true
		});
	},
	update : function(cid, comment) {
		$.ajax({
			dataType : "json",
			type : "POST",
			data : {
				"cid" : cid,
				"content" : comment
			},
			url : "/app/comment!updateComment",
			success : function(data) {
				backUrl.addCookie(data.state);
				if ('success' == data.state) {
					commentList.get();
					tipsAlert(data.msg);
				} else if ('error' == data.state) {
					alert(data.msg);
				}
			}
		});
	}
};
var commentDel = function(cid) {
	if (confirm("您确定要删除该批示吗？")) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"cid" : cid
			},
			url : "/app/comment!delComment",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state = "success") {
					commentList.get();
					tipsAlert("已删除");
				} else if (data.state == "error") {
					alert("删除失败");
				}
			}
		});
	}
};
var commentAdd = function(pid) {
	var content = $("#comment").val();
	$.ajax({
		dataType : "json",
		type : "post",
		data : {
			"pid" : pid,
			"content" : content
		},
		url : "/app/comment!addComment",
		success : function(data) {
			backUrl.addCookie(data.state);
			if (data.state = "success") {
				commentList.get();
				$("#comment").val("");
				tipsAlert("已添加");
			} else if (data.state == "error") {
				alert("添加失败");
			}
		}
	});
};